import wx from '../utils/wx'
export default {
  data() {
    return {
      isPageEmpty: false,
      emptyMsg: '',
      hidBtn: false,
      scrollHeight: 0,
      itemWidth: 0,
      itemHeight: 0,
      scrollTop: 100,
      isLoadingMore: false,
      isReferesh: false,
      hasMore: false,
      loadMoreText: '',
      footerHide: true,
      headerHide: true,
      footerAnimationData: {},
      headerAnimationData: {},
      paramsDatas: {
        pageIndex: 1,
        pageSize: 10
      }
    }
  },
  onPullDownRefresh() {
    this.upper()
  },
  onReachBottom() {
    this.lower()
  },
  methods: {
    handlerFail(isRefresh,resData) {
      if (isRefresh) {
        this.headerHide = true
        this.isReferesh = false
        this.hideHeaderAnimation()
        this.isPageEmpty = true
        this.hidBtn = false
        this.emptyMsg = resData.message
      } else {
        this.footerHide = true
        this.isLoadingMore = false
      }
    },
    handlerNotCurrentPage(){
      this.headerHide = true
      this.isReferesh = false
      this.footerHide = true
      this.isLoadingMore = false
    },
    preHandler(isRefresh, tempSize) {
      wx.stopPullDownRefresh();
      if (tempSize < this.paramsDatas.pageSize) {
        this.hasMore = false
      } else {
        this.hasMore = true
      }
      if (isRefresh) {
        this.headerHide = true
        this.isReferesh = false
        this.hideHeaderAnimation()
        if (this.hasMore){
          this.paramsDatas.pageIndex++
        }
      } else {
        if (this.hasMore) {
          this.paramsDatas.pageIndex++
          this.footerHide = true
        } else {
          if (this.paramsDatas.pageIndex == 1){
            this.footerHide = true
            this.loadMoreText = ""
          } else{
            this.footerHide = false
            this.loadMoreText = "我是有底线的"
          }
          
        }
      }
      this.isLoadingMore = false
    },
    upper(e) {
      if (this.isReferesh || this.isLoadingMore) {
        return
      }
      this.headerHide = false
      this.isReferesh = true
      this.startShowHeaderAnimation()
      let self = this
      setTimeout(function () {
        self.onRefresh(true)
      }, 1000)
    },
    lower(e) {
      if (this.isLoadingMore || this.isReferesh) {
        return
      }
      if (!this.hasMore) {
        if (this.paramsDatas.pageIndex == 1) {
          this.footerHide = true
          this.loadMoreText = ""
        } else {
          this.footerHide = false
          this.loadMoreText = "我是有底线的"
        }
        return
      }
      this.loadMoreText = ""
      this.footerHide = false
      this.isLoadingMore = true
      let self = this
      setTimeout(function () {
        self.onRefresh(false)
      }, 1000)
    },
    scroll(e) {
      console.log("scroll===")
    },
    startShowHeaderAnimation() {
      console.log("startShowHeaderAnimation===")
      const animation = wx.createAnimation({
        duration: 1500,
        timingFunction: 'linear'
      })
      animation.translateY(50).step()
      this.headerAnimationData = animation.export()
    },
    hideHeaderAnimation() {
      const animation = wx.createAnimation({
        duration: 1500,
        timingFunction: 'linear'
      })
      animation.translateY(-50).step()
      this.headerAnimationData = animation.export()
    }
  }
}
